package com.ynny.demo5.dao;

import com.ynny.demo5.bean.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class StudentDaoImpl implements studentDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int insertStudent(String number, String name, String gender, String clbum, String grade) {
        String sql = "insert into students(number,name,gender,clbum,grade) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,number,name,gender,clbum,grade);

        return i;
    }

    @Override
    public int updateStudents(String newnumber, String newgender, String newclbum, String newgrade, String name) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql,newnumber,newgender,newclbum,newgrade,name);
        if(i>0){
            System.out.println("修改成功");
        }

        return i;
    }

    @Override
    public List<student> selectAll() {
        String sql = "select * from students";
        List<student> yyy = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(student.class));


        return yyy;
    }

    @Override
    public student getStudents(String number) {
        String sql = "select * from students where number=?";
        student students = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<student>(student.class), number);
        if(students!=null){
            return students;
        }

        return students;
    }

    @Override
    public int update(student students) {
        String sql = "update students set number=?,gender=?,clbum=?,grade=? where name=?";
        int i = jdbcTemplate.update(sql, students.getNumber(), students.getGender(), students.getClbum(), students.getGrade() , students.getName());
        return i;
    }
}
